package com.rzico.sbl.ui.report

import android.widget.ImageView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportTotalBinding
import com.rzico.sbl.model.HomeContent
import com.rzico.sbl.other.*
import com.rzico.sbl.viewmodel.WaterViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.*
import com.xinnuo.common_ui.glideExt.loadImage
import com.xinnuo.common_ui.utils.showTimeRangeDialog
import com.xinnuo.slimadapter.SlimAdapter

class ReportTotalActivity : BaseActivity(R.layout.activity_report_total) {

    private val mBinding by lazy { ActivityReportTotalBinding.bind(getRootView()) }

    private var mDateMode = "4"
    private var mBeginDate = getFirstDate()
    private var mEndDate = getEndDate()

    override fun getViewModel() = get<WaterViewModel>()

    override fun initData() {
        initTitle(title = "商品统计")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关统计信息！"
            }

            recycleList.also { layout ->
                layout.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        isLoadingMore
                            .conditionFalse {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                    }
                )

                layout.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                mAdapter = SlimAdapter.creator()
                    .register<HomeContent>(R.layout.item_report_station_goods) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_goods_name, bean.name)
                            text(R.id.item_goods_type, "规格：${bean.spec}")
                            text(R.id.item_goods_price, "单价：${bean.price.formatDecimal()} 元")
                            text(R.id.item_goods_num, bean.quantity)

                            with<ImageView>(R.id.item_goods_logo) {
                                it.loadImage(bean.thumbnail)
                            }

                            clicked {
                                startActivityEx<ReportTotalRecordActivity>(
                                    "shopId" to intent.getExtra("shopId"),
                                    "productId" to bean.product,
                                    "dateMode" to mDateMode,
                                    "beginDate" to mBeginDate,
                                    "endDate" to mEndDate
                                )
                            }
                        }
                    }
                    .attachTo(layout)
            }
        }
    }

    override fun initListener() {
        mBinding
            .totalTime
            .apply {
                getViewModel().mUncheckTimeList[3].isChecked = true
                timeHint.text = getViewModel().mUncheckTimeList[3].hint

                timeLayout.oneClick {
                    getViewModel()
                        .showDropTimeFilter(
                            getIContext(),
                            timeHint,
                            timeHintArrow,
                            timeLayout,
                            getViewModel().mTimeList
                        ) { bean ->

                            if (bean.id != "7")
                                getViewModel()
                                    .mTimeList
                                    .last()
                                    .name = "自定义时间"

                            when (bean.id) {
                                "1" -> {
                                    mDateMode = "1"
                                    mBeginDate = getTodayDate()
                                    mEndDate = getTodayDate()

                                    updateList()
                                }

                                "2" -> {
                                    mDateMode = "2"
                                    mBeginDate = getBeforeDate()
                                    mEndDate = getBeforeDate()

                                    updateList()
                                }

                                "3" -> {
                                    mDateMode = "3"
                                    mBeginDate = getMondayDate()
                                    mEndDate = getSundayDate()

                                    updateList()
                                }

                                "4" -> {
                                    mDateMode = "4"
                                    mBeginDate = getFirstDate()
                                    mEndDate = getEndDate()

                                    updateList()
                                }

                                "5" -> {
                                    mDateMode = "5"
                                    mBeginDate = getBeforeFirstDate()
                                    mEndDate = getBeforeEndDate()

                                    updateList()
                                }

                                "6" -> {
                                    mDateMode = "6"
                                    mBeginDate = getYearFirstDate()
                                    mEndDate = getYearEndDate()

                                    updateList()
                                }

                                "7" -> {
                                    showTimeRangeDialog(
                                        isCurrentDate = true,
                                        isAfterLimited = true,
                                        onResult = { start, end ->
                                            getViewModel()
                                                .mTimeList
                                                .forEach { it.isChecked = false }

                                            getViewModel()
                                                .mTimeList
                                                .last()
                                                .isChecked = true

                                            mDateMode = "7"
                                            mBeginDate = start
                                            mEndDate = end
                                            bean.name = "$mBeginDate ~ $mEndDate"
                                            timeHint.text = bean.name

                                            updateList()
                                        }
                                    )
                                }
                            }
                        }
                }
            }
    }

    /**
     * 商品统计一级列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .shopSummary(
                page = index,
                shopId = intent.getExtra("shopId"),
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = { bean ->
                    bean?.let {
                        mList.apply {
                            (index == 0).condition {
                                clear()
                                pageNum = 0
                            }

                            addItems(it.data)
                            it.data.notEmpty { pageNum++ }
                        }

                        it.summary
                            .isNotEmpty()
                            .conditionElse(
                                actionTrue = {
                                    mBinding.totalCount.text = it.summary[0].amount
                                    mBinding.totalMoney.text = it.summary[0].price.formatDecimal()
                                },
                                actionFalse = {
                                    mBinding.totalCount.text = "0"
                                    mBinding.totalMoney.text = "0.00"
                                }
                            )

                        mAdapter.setDataList(mList)
                    }
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}